package com.nufront.bloodsugar.view;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import com.nufront.bloodsugar.R;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

public class LineChartOld extends LinearLayout {

	private Context mContext;
	private LayoutInflater layoutInflater;
	private int length;
	private View chart;
	private String[] titles = new String[] { "1", "2", "3" };
	private List<double[]> xList = new ArrayList<double[]>();
	private List<double[]> yList = new ArrayList<double[]>();
	List<double[]> values = new ArrayList<double[]>();

	public LineChartOld(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mContext = context;

		init();
		this.addView(chart);
	}

	public LineChartOld(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	private void init() {
		layoutInflater = LayoutInflater.from(mContext);

		xList.add(new double[] { 1, 3, 5, 7, 9, 11 });
		xList.add(new double[] { 0, 2, 4, 6, 8, 10 });
		xList.add(new double[] { 0, 2, 3, 5, 6, 7 });

		yList.add(new double[] { 3, 14, 5, 30, 20, 25 });
		yList.add(new double[] { 18, 9, 21, 15, 10, 6 });
		yList.add(new double[] { 10, 5, 17, 11, 25, 18 });

		XYMultipleSeriesDataset dataset = buildDataset(titles, xList, yList);

		int colorPoint = Color.rgb(168, 68, 30);// #ab4c1e

		int[] colors = new int[] { Color.BLUE, colorPoint, Color.RED };
		PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE,
				PointStyle.DIAMOND, PointStyle.TRIANGLE };
		XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles, true);

		setChartSettings(renderer, "", "X", "Y", -1, 12, 0, 35, Color.BLACK,
				Color.BLACK, Color.TRANSPARENT);

		length = renderer.getSeriesRendererCount();
		for (int i = 0; i < length; i++) {
			SimpleSeriesRenderer seriesRenderer = renderer
					.getSeriesRendererAt(i);
			seriesRenderer.setDisplayChartValues(true);
		}

		chart = ChartFactory.getLineChartView(mContext, dataset, renderer);

		findMyView();
		setMyClickListener();
	}

	private void findMyView() {

	}

	private void setMyClickListener() {

	}

	protected XYMultipleSeriesDataset buildDataset(String[] titles,
			List<double[]> xValues, List<double[]> yValues) {

		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

		int length = titles.length; //
		for (int i = 0; i < length; i++) {
			XYSeries series = new XYSeries(titles[i]); //
			double[] xV = xValues.get(i); //
			double[] yV = yValues.get(i);
			int seriesLength = xV.length; //
			for (int k = 0; k < seriesLength; k++) //
			{
				series.add(xV[k], yV[k]);
			}

			dataset.addSeries(series);
		}

		return dataset;

	}

	protected XYMultipleSeriesRenderer buildRenderer(int[] colors,
			PointStyle[] styles, boolean fill) {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		int length = colors.length;
		for (int i = 0; i < length; i++) {
			XYSeriesRenderer r = new XYSeriesRenderer();
			r.setColor(colors[i]);
			r.setPointStyle(styles[i]);
			r.setFillPoints(fill);
			renderer.addSeriesRenderer(r);
		}
		return renderer;
	}

	/**
	 * 
	 * @param renderer
	 * @param title
	 * @param xTitle
	 * @param yTitle
	 * @param xMin
	 * @param xMax
	 * @param yMin
	 * @param yMax
	 * @param axesColor
	 * @param labelsColor
	 */
	protected void setChartSettings(XYMultipleSeriesRenderer renderer,
			String title, String xTitle, String yTitle, double xMin,
			double xMax, double yMin, double yMax, int axesColor,
			int labelsColor, int backgroundColor) {
		// renderer.setChartTitle(title);
		// renderer.setXTitle(xTitle);
		// renderer.setYTitle(yTitle);
		// renderer.setXAxisMin(xMin);
		// renderer.setXAxisMax(xMax);
		renderer.setYAxisMin(yMin);
		renderer.setYAxisMax(yMax);
		// renderer.setXLabels(13);
		// renderer.setYLabels(7);
		renderer.setShowAxes(false);
		renderer.setShowGrid(false);
		renderer.setShowLabels(false);
		renderer.setShowLegend(false);
		renderer.setXLabelsAlign(Align.RIGHT);
		renderer.setYLabelsAlign(Align.RIGHT);
		renderer.setZoomButtonsVisible(false);
		renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
		renderer.setZoomLimits(new double[] { -10, 20, -10, 20 });

		// renderer.setAxesColor(axesColor);
		renderer.setApplyBackgroundColor(true);
		renderer.setBackgroundColor(backgroundColor);
		renderer.setMargins(new int[] { 0, 0, 0, 0 });
		renderer.setMarginsColor(Color.BLUE);
		// renderer.setLabelsColor(labelsColor);
	}

}
